HA$PBExportHeader$w_sort.srw
forward
global type w_sort from window
end type
type cb_2 from commandbutton within w_sort
end type
type cb_1 from commandbutton within w_sort
end type
type dw_2 from datawindow within w_sort
end type
type dw_1 from datawindow within w_sort
end type
end forward

global type w_sort from window
integer width = 2345
integer height = 792
boolean titlebar = true
string title = "Ordenar"
boolean controlmenu = true
boolean minbox = true
windowtype windowtype = popup!
long backcolor = 67108864
string icon = "AppIcon!"
boolean center = true
cb_2 cb_2
cb_1 cb_1
dw_2 dw_2
dw_1 dw_1
end type
global w_sort w_sort

type variables
s_parm istr_parm
String is_sort_inicial = ''
end variables

forward prototypes
public subroutine of_inicializar (datawindow adw)
public subroutine of_sort_inicial (datawindow adw)
end prototypes

public subroutine of_inicializar (datawindow adw);string ls_syntax, ls_cadena, ls_temp
long ld_pos, ld_pos_ini, ld_pos_fin
Integer li_fila
ls_syntax = adw.Describe("DataWindow.Syntax")

/* Recupera el nombre de las columnas */
ld_pos = 1
ls_cadena = ''
Do While ld_pos > 0
	ld_pos = pos(ls_syntax, 'text', ld_pos)
	If ld_pos > 0 Then
		ld_pos_ini = pos(ls_syntax, 'name=', ld_pos)
		ld_pos_fin = pos(ls_syntax, ' ', ld_pos_ini)
		ls_temp = ls_cadena +' '+ mid(ls_syntax, ld_pos_ini+5, ld_pos_fin - ld_pos_ini - 5 )
		If Right(ls_temp, 2)='_t' Then 	
			li_fila = dw_1.InsertRow(0)
			dw_1.Object.Columna_name[li_fila] = Trim(Left(ls_temp, Len(ls_temp)-2))
					
			//-- las etiquetas
			ld_pos_ini = pos(ls_syntax, 'text="', ld_pos_fin)
			ld_pos_fin = pos(ls_syntax, '"', ld_pos_ini+7)
			
			dw_1.Object.Columna_text[li_fila] = Trim(mid(ls_syntax, ld_pos_ini+6, ld_pos_fin - ld_pos_ini -6))
		
		End If
			
		ld_pos = ld_pos_fin
	End If
Loop

Return
end subroutine

public subroutine of_sort_inicial (datawindow adw);string ls_syntax, ls_cadena, ls_temp
long ld_pos, ld_pos_ini, ld_pos_fin
Integer li_fila
ls_syntax = adw.Describe("DataWindow.Syntax")

ld_pos = 1
Do While ld_pos > 0
	ld_pos = pos(ls_syntax, 'group', ld_pos)
	If ld_pos > 0 Then
		ld_pos_ini = pos(ls_syntax, 'by=(', ld_pos)
		ld_pos_fin = pos(ls_syntax, ')', ld_pos_ini)
		ls_cadena = ls_cadena + mid(ls_syntax, ld_pos_ini+4, ld_pos_fin - ld_pos_ini - 4)
		ld_pos = ld_pos_fin
	End If
Loop

ld_pos = 1
Do While ld_pos > 0
	ld_pos_ini = pos(ls_cadena, '"', ld_pos)
	ld_pos_fin = pos(ls_cadena, '"', ld_pos_ini+1)
	ls_temp = mid(ls_cadena, ld_pos_ini+1, ld_pos_fin - ld_pos_ini -1)
	
	If Trim(ls_temp)<>'' Then is_sort_inicial = is_sort_inicial + ls_temp + ' ,'
	
	ld_pos = ld_pos_fin
	
	/*
	//-- Busca la etiqueta
	li_fila = dw_1.Find('columna_name=~''+ls_temp+'~'',1, dw_1.RowCount())
	If li_fila > 0 Then
		dw_2.InsertRow(0)
		dw_2.Object.Columna_name[dw_2.RowCount()] = dw_1.object.Columna_name[li_fila]
		dw_2.Object.Columna_text[dw_2.RowCount()] = dw_1.object.Columna_Text[li_fila]
		dw_2.Object.cb_protec[dw_2.RowCount()]='1'
	End If

	*/
Loop




end subroutine

on w_sort.create
this.cb_2=create cb_2
this.cb_1=create cb_1
this.dw_2=create dw_2
this.dw_1=create dw_1
this.Control[]={this.cb_2,&
this.cb_1,&
this.dw_2,&
this.dw_1}
end on

on w_sort.destroy
destroy(this.cb_2)
destroy(this.cb_1)
destroy(this.dw_2)
destroy(this.dw_1)
end on

event open;istr_parm=message.powerobjectparm

this.function dynamic of_inicializar(istr_parm.dw_parm[1])
this.function dynamic of_sort_inicial(istr_parm.dw_parm[1])
end event

type cb_2 from commandbutton within w_sort
integer x = 1906
integer y = 572
integer width = 402
integer height = 112
integer taborder = 40
integer textsize = -8
integer weight = 400
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Arial"
string text = "Cancelar"
end type

event clicked;parent.Hide()
end event

type cb_1 from commandbutton within w_sort
integer x = 1477
integer y = 572
integer width = 402
integer height = 112
integer taborder = 30
integer textsize = -8
integer weight = 400
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Arial"
string text = "Aceptar"
end type

event clicked;istr_parm.string_parm[5]=""

for istr_parm.long_parm[1]=1 to dw_2.rowcount()
	 istr_parm.string_parm[5]=istr_parm.string_parm[5] + &
							   	  dw_2.getitemstring(istr_parm.long_parm[1],"columna_name")
    if dw_2.getitemstring(istr_parm.long_parm[1],"columna_sort")='1' then
		 istr_parm.string_parm[5]=istr_parm.string_parm[5] + " A ,"  
	else
		 istr_parm.string_parm[5]=istr_parm.string_parm[5] + " D ,"  		
	end if
next

istr_parm.string_parm[5]=left(istr_parm.string_parm[5],len(istr_parm.string_parm[5]) - 1)
istr_parm.string_parm[5] = is_sort_inicial + istr_parm.string_parm[5]
istr_parm.dw_parm[1].setsort(istr_parm.string_parm[5])
istr_parm.dw_parm[1].sort()
istr_parm.dw_parm[1].GroupCalc()
parent.Hide()
end event

type dw_2 from datawindow within w_sort
event ue_lbuttondown pbm_lbuttondown
integer x = 1207
integer y = 8
integer width = 1111
integer height = 536
integer taborder = 20
string title = "none"
string dataobject = "d_sort_column"
boolean vscrollbar = true
boolean livescroll = true
borderstyle borderstyle = stylelowered!
end type

event ue_lbuttondown;this.dragicon="drag.ico"
this.drag(begin!)
end event

event clicked;if dwo.name='columna_sort' then
	if this.getitemstring(row,'columna_sort')='1' then
		this.setitem(row,'columna_sort','0')
	else
		this.setitem(row,'columna_sort','1')		
	end if
end if
end event

event dragdrop;long ll_fila

if classname(source)="dw_1" then
	ll_fila=dw_2.insertrow(0)
	dw_2.setitem(ll_fila,"columna_name",dw_1.getitemstring(dw_1.getrow(),"columna_name"))
	dw_2.setitem(ll_fila,"columna_text",dw_1.getitemstring(dw_1.getrow(),"columna_text"))
	dw_2.setitem(ll_fila,"columna_sort","1")
	//dw_1.deleterow(0)
end if

end event

event rowfocuschanged;if currentrow < 1 then return 1

this.selectrow(0,FALSE)
this.selectrow(currentrow,TRUE)
end event

type dw_1 from datawindow within w_sort
event ue_lbuttondown pbm_lbuttondown
integer x = 23
integer y = 12
integer width = 1152
integer height = 520
integer taborder = 10
string title = "none"
string dataobject = "d_sort_column_descripcion"
boolean vscrollbar = true
boolean livescroll = true
borderstyle borderstyle = stylelowered!
end type

event ue_lbuttondown;This.dragicon="drag.ico"
This.drag(begin!)

end event

event dragdrop;long ll_fila

if classname(source)="dw_2" then
	dw_2.deleterow(0)
end if
	

end event

event rowfocuschanged;if currentrow < 1 then return
this.selectrow(0,FALSE)
this.selectrow(currentrow,TRUE)

end event

